#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),
    m_contentChanged(false)
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    transq.load("qtmindmap_nb_NO.qm",":/others");
    //    setup ui
    m_ui->setupUi(this);

    connect(m_ui->actionNew, SIGNAL(activated()), this, SLOT(newFile()));
    connect(m_ui->actionOpen, SIGNAL(activated()), this, SLOT(openFile()));
    connect(m_ui->actionSave, SIGNAL(activated()), this, SLOT(saveFile()));
    connect(m_ui->actionSaveAs, SIGNAL(activated()), this, SLOT(saveFileAs()));
    connect(m_ui->actionClose, SIGNAL(activated()), this, SLOT(closeFile()));
    connect(m_ui->actionExport, SIGNAL(activated()), this, SLOT(exportScene()));
    connect(m_ui->actionQuit, SIGNAL(activated()), this, SLOT(quit()));
    connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()),
            this, SLOT(about()));

    // graphwidget is hided by def, new/open file will show it
    m_graphicsView = new GraphWidget(this);
    m_treeWidget = new QTreeWidget(this);
    m_treeWidget->setHeaderLabel(tr("Objects manager"));

    m_ui->horizontalLayout->addWidget(m_graphicsView);
    m_ui->horizontalLayout->addWidget(m_treeWidget);
    m_treeWidget->setVisible(false);
    m_treeWidget->setMinimumWidth(205);





    //    setCentralWidget(m_graphicsView);
    m_graphicsView->hide();


    connect(m_graphicsView->graphLogic(), SIGNAL(contentChanged(const bool&)),
            this, SLOT(contentChanged(const bool&)));

    connect(m_graphicsView, SIGNAL(notification(QString)),
            this, SLOT(statusBarMsg(QString)));

    connect(m_graphicsView->graphLogic(), SIGNAL(notification(QString)),
            this, SLOT(statusBarMsg(QString)));

    connect(m_graphicsView->graphLogic(), SIGNAL(sendTreeWidget(QTreeWidget*)),
            this,SLOT(setTreeWidget(QTreeWidget*)));


    // setup toolbars, don't show them
    setupMainToolbar();
    m_ui->mainToolBar->hide();

    setupStatusIconToolbar();
    m_ui->statusIcons_toolBar->hide();

    setupEditToolbar();
    m_ui->undoToolBar->hide();

    refreash();
}

void MainWindow::setTreeWidget(QTreeWidget *tree) {
    treeMain = tree;
    m_treeWidget->clear();
    QTreeWidgetItem* pW01 = new QTreeWidgetItem(m_treeWidget);
    pW01->setText(0,"");
    QTreeWidgetItem* pW02 = new QTreeWidgetItem(m_treeWidget);
    pW02->setText(0,"");

    buttonExpan = new QPushButton(tr("Expand all"));
    connect(buttonExpan,SIGNAL(clicked()),this,SLOT(expandAll()));
    buttonCollaps = new QPushButton(tr("Collaps all"));
    connect(buttonCollaps,SIGNAL(clicked()),this,SLOT(collapsAll()));

    m_treeWidget->setItemWidget(pW01,0,buttonExpan);
    m_treeWidget->setItemWidget(pW02,0,buttonCollaps);
    QTreeWidgetItem* pW0 = new QTreeWidgetItem(m_treeWidget);
    pW0->setText(0,"Hop");
    m_treeWidget->setItemWidget(pW0,0,treeMain);
    m_treeWidget->expandAll();
}

void MainWindow::expandAll() {
    treeMain->expandAll();
}

void MainWindow::collapsAll() {
    treeMain->collapseAll();
}

void MainWindow::hideTree() {
    if(m_treeWidget->isVisible()) {
        m_hideTree->setEnabled(false);
        m_treeWidget->setVisible(false);
    }
    else
        m_hideTree->setEnabled(false);
    m_showTreeManager->setIcon(QIcon(":/network.png"));
}

MainWindow::~MainWindow()
{
    delete m_ui;
}

void MainWindow::statusBarMsg(const QString &msg)
{
    m_ui->statusBar->showMessage(msg, 5000);
}

void MainWindow::contentChanged(const bool& changed)
{
    // only care about the transitions
    if (m_contentChanged == false && changed == true)
    {
        setWindowTitle(windowTitle().prepend("* "));
        m_contentChanged = true;

        QFileInfo fileInfo(m_fileName);
        if (m_fileName != tr("untitled") && fileInfo.isWritable())
            m_ui->actionSave->setEnabled(true);
    }
    else if (m_contentChanged == true && changed == false)
    {
        setWindowTitle(windowTitle().remove(0,2));
        m_contentChanged = false;
        m_ui->actionSave->setEnabled(false);
    }
}

void MainWindow::newFile()
{
    if (!closeFile())
        return;

    m_graphicsView->newScene();

    m_ui->actionSave->setEnabled(false);
    m_ui->actionSaveAs->setEnabled(true);
    m_ui->actionClose->setEnabled(true);
    m_ui->actionExport->setEnabled(true);
    contentChanged(false);
    m_fileName = tr("untitled");
    setTitle(m_fileName);

    m_graphicsView->setFocus();

    showMainToolbar();
}

void MainWindow::openFile(const QString &fileName)
{
    if (!closeFile())
        return;

    QString currFilename(m_fileName);
    if (fileName.isEmpty())
    {
        QFileDialog dialog(this,
                           tr("Open MindMap"),
                           QDir::homePath(),
                           QString("QtMindMap (*.qmm)"));
        dialog.setAcceptMode(QFileDialog::AcceptOpen);
        dialog.setDefaultSuffix("qmm");

        if (!dialog.exec())
            return;

        m_fileName = dialog.selectedFiles().first();
    }
    else
    {
        m_fileName = fileName;
    }

    QFileInfo fileInfo(m_fileName);
    if (!fileInfo.isWritable())
        statusBarMsg(tr("Read-only file!"));

    if (!m_graphicsView->graphLogic()->readContentFromXmlFile(m_fileName))
    {
        m_fileName = currFilename;
        return;
    }


    m_ui->actionSaveAs->setEnabled(true);
    m_ui->actionClose->setEnabled(true);
    m_ui->actionExport->setEnabled(true);
    m_ui->actionSave->setEnabled(false);
    m_ui->actionSave->setEnabled(false);

    contentChanged(false);

    fileInfo.isWritable() ?
                setTitle(m_fileName) :
                setTitle(tr("readonly ").append(m_fileName));

    showMainToolbar();
}

void MainWindow::saveFile(const bool &checkIfReadonly)
{
    QFileInfo fileInfo(m_fileName);
    if (checkIfReadonly && !fileInfo.isWritable())
    {
        statusBarMsg(tr("Read-only file!"));
        return;
    }

    m_graphicsView->graphLogic()->writeContentToXmlFile(m_fileName);
    contentChanged(false);

    m_undoStack->clear();
}

bool MainWindow::saveFileAs()
{
    QFileDialog dialog(this,
                       tr("Save MindMap as"),
                       QDir::homePath(),
                       QString("QtMindMap (*.qmm)"));
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    dialog.setDefaultSuffix("qmm");

    if (!dialog.exec())
        return false;

    m_fileName = dialog.selectedFiles().first();
    saveFile(false);
    setTitle(m_fileName);
    return true;
}

bool MainWindow::closeFile()
{
    if (m_contentChanged)
    {
        QMessageBox msgBox(this);
        msgBox.setWindowTitle(tr("Save mindmap - qtmindmap"));
        msgBox.setText(tr("The mindmap has been modified."));
        msgBox.setInformativeText(tr("Do you want to save your changes?"));
        msgBox.setStandardButtons(QMessageBox::Save |
                                  QMessageBox::Discard |
                                  QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Save);
        int ret = msgBox.exec();

        switch (ret) {
        case QMessageBox::Save:
        {
            if (m_fileName == tr("untitled"))
            {
                if (!saveFileAs())
                    return false;
            }
            else
            {
                saveFile();
            }

            break;
        }
        case QMessageBox::Discard:
            break;
        case QMessageBox::Cancel:
            return false;
        default:
            break;
        }
    }

    m_ui->actionSave->setEnabled(false);
    m_ui->actionSaveAs->setEnabled(false);
    m_ui->actionClose->setEnabled(false);
    m_ui->actionExport->setEnabled(false);
    m_contentChanged = false;
    setTitle("");
    m_graphicsView->closeScene();
    m_undoStack->clear();
    showMainToolbar(false);
    showUndoToolbar(false);
    return true;
}

void MainWindow::exportScene()
{
    QFileDialog dialog(this,
                       tr("Export MindMap to image"),
                       QDir::homePath(),
                       tr("PNG image file (*.png)"));
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    dialog.setDefaultSuffix("png");

    if (dialog.exec())
    {
        m_graphicsView->graphLogic()->writeContentToPngFile(
                    dialog.selectedFiles().first());
    }
}

void MainWindow::about()
{
    QMessageBox msgBox(this);
    msgBox.setWindowTitle(tr("About %1").arg(windowTitle()));
    msgBox.setText(tr("%1 software written in Qt.").arg(windowTitle()));
    msgBox.setTextFormat(Qt::RichText);
    msgBox.setInformativeText(tr("very vounderful"));
    QPixmap pixMap(":/qtmindmap.svg");
    msgBox.setIconPixmap(pixMap.scaled(50,50));
    msgBox.exec();
}

void MainWindow::showMainToolbar(const bool &show)
{
    m_ui->mainToolBar->setVisible(show ?
                                      !m_ui->mainToolBar->isVisible() :
                                      false);
}

void MainWindow::showStatusIconToolbar(const bool &show)
{
    m_ui->statusIcons_toolBar->setVisible(
                show ?
                    !m_ui->statusIcons_toolBar->isVisible() :
                    false);
}

void MainWindow::showUndoToolbar(const bool &show)
{
    m_ui->undoToolBar->setVisible(show ?
                                      !m_ui->undoToolBar->isVisible() :
                                      false);
}

void MainWindow::quit()
{
    if (m_contentChanged && !closeFile())
        return;

    QApplication::instance()->quit();
}

void MainWindow::closeEvent(QCloseEvent * event)
{
    m_contentChanged && !closeFile() ?
                event->ignore() :
                event->accept();
}

void MainWindow::setupMainToolbar()
{
    // why can't I do this with qtcreator? (adding actions to toolbar)


    /// @bug or a feature? no underline here
    m_addNode = new QAction(QIcon(":/addTop.png"),tr("Add node (ins)"), this);
    connect(m_addNode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(insertNode()));

    m_delNode = new QAction(QIcon(":/delTop.png"),tr("Del node (del)"), this);
    connect(m_delNode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(removeNode()));

    m_editNode = new QAction(tr("Edit node (F2, dubclick)"), this);
    connect(m_editNode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(nodeEdited()));

    m_scaleUpNode = new QAction(tr("ScaleUp Node (Ctrl +)"), this);
    connect(m_scaleUpNode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(scaleUp()));
    m_scaleDownNode = new QAction(tr("ScaleDown Node (Ctrl -)"), this);
    connect(m_scaleDownNode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(scaleDown()));

    m_nodeColor = new QAction(QIcon(":/color.png"),tr("Node color (c)"), this);
    connect(m_nodeColor, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(nodeColor()));

    m_nodeTextColor = new QAction(tr("Node textcolor (t)"), this);
    connect(m_nodeTextColor, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(nodeTextColor()));

    m_addEdge = new QAction(QIcon(":/addArc.png"),tr("Add edge (a)"), this);
    connect(m_addEdge, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(addEdge()));

    m_delEdge = new QAction(QIcon(":/delArc.png"),tr("Del edge (d)"), this);
    connect(m_delEdge, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(removeEdge()));

    m_moveNode = new QAction(tr("Move node\n(Ctrl cursor, drag)"), this);
    m_moveNode->setDisabled(true);

    m_subtree = new QAction(tr("Change on wholesubtree\n(Ctrl shift)"), this);
    m_subtree->setDisabled(true);

    m_zoomIn = new QAction(tr("Zoom in (+, scrollup)"), this);
    connect(m_zoomIn, SIGNAL(activated()), m_graphicsView,
            SLOT(zoomIn()));
    m_zoomIn->setIcon(QIcon(":/zoomIn.png"));

    m_zoomOut = new QAction(tr("Zoom out (-, scrolldown)"), this);
    connect(m_zoomOut, SIGNAL(activated()), m_graphicsView,
            SLOT(zoomOut()));
    m_zoomOut->setIcon(QIcon(":/zoomOut.png"));

    m_esc = new QAction(tr("Leave editing,\nedge eadd/remove (esc)"), this);
    connect(m_esc, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(nodeLostFocus()));

    m_hintMode = new QAction(tr("Hint mode (f)"), this);
    connect(m_hintMode, SIGNAL(activated()), m_graphicsView->graphLogic(),
            SLOT(hintMode()));

    m_showMainToolbar = new QAction(tr("Show main toolbar\n(Ctrl m)"), this);
    connect(m_showMainToolbar, SIGNAL(activated()), this,
            SLOT(showMainToolbar()));

    m_showStatusIconToolbar = new QAction(tr("Insert status icons\n(Ctrl i)"),
                                          this);
    connect(m_showStatusIconToolbar, SIGNAL(activated()), this,
            SLOT(showStatusIconToolbar()));

    m_showTreeManager = new QAction(QIcon(":/network.png"),tr("Show tree manager"), this);
    connect(m_showTreeManager, SIGNAL(activated()), this,
            SLOT(showTreeManager()));

    m_hideTree = new  QAction(tr("Hide tree manager"), this);
    connect(m_hideTree,SIGNAL(triggered()),this,SLOT(hideTree()));
    m_hideTree->setEnabled(false);

    m_startMlv = new QAction(tr("Run inference engine"),this);
    connect(m_startMlv,SIGNAL(triggered()),this,
            SLOT(readQuestions()));

    m_setEnglish = new QAction(QIcon(":/en.png"),tr("English"), this);
    connect(m_setEnglish,SIGNAL(triggered()),this,SLOT(setEnglishLanguage()));

    m_setRussian = new QAction(QIcon(":/ru.png"),tr("Русский"), this);
    connect(m_setRussian,SIGNAL(triggered()),this,SLOT(setRussionLanguage()));


    m_ui->actionNew->setIcon(QIcon(":/new.png"));
    m_ui->actionOpen->setIcon(QIcon(":/open.png"));
    m_ui->actionSave->setIcon(QIcon(":/save.png"));

    m_ui->actionHelp->setIcon(QIcon(":/help1.png"));

    //Меню вершина
    m_ui->menuNode->addAction(m_addNode);
    m_ui->menuNode->addAction(m_delNode);
    m_ui->menuNode->addAction(m_editNode);
    m_ui->menuNode->addAction(m_moveNode);
    m_ui->menuNode->addSeparator();
    m_ui->menuNode->addAction(m_nodeColor);
    m_ui->menuNode->addAction(m_nodeTextColor);
    m_ui->menuNode->addSeparator();
    m_ui->menuNode->addAction(m_scaleUpNode);
    m_ui->menuNode->addAction(m_scaleDownNode);

    //Меню сеть
    m_ui->menuNet->addAction(m_addEdge);
    m_ui->menuNet->addAction(m_delEdge);
    m_ui->menuNet->addAction(m_esc);
    m_ui->menuNet->addAction(m_subtree);
    m_ui->menuNet->addSeparator();
    m_ui->menuNet->addAction(m_showTreeManager);
    m_ui->menuNet->addAction(m_hideTree);


    //Вид
    m_ui->menuView->addAction(m_zoomIn);
    m_ui->menuView->addAction(m_zoomOut);
    m_ui->menuView->addAction(m_hintMode);
    m_ui->menuView->addSeparator();
    m_ui->menuView->addAction(m_setEnglish);
    m_ui->menuView->addAction(m_setRussian);

    //Добавляем в tool bar
    m_ui->toolBar->addAction(m_ui->actionNew);
    m_ui->toolBar->addAction(m_ui->actionOpen);
    m_ui->toolBar->addAction(m_ui->actionSave);
    m_ui->toolBar->addSeparator();
    m_ui->toolBar->addAction(m_addNode);
    m_ui->toolBar->addAction(m_delNode);
    m_ui->toolBar->addAction(m_nodeColor);
    m_ui->toolBar->addSeparator();
    m_ui->toolBar->addAction(m_addEdge);
    m_ui->toolBar->addAction(m_delEdge);
    m_ui->toolBar->addSeparator();
    m_ui->toolBar->addAction(m_zoomIn);
    m_ui->toolBar->addAction(m_zoomOut);
    m_ui->toolBar->addSeparator();
    m_ui->toolBar->addAction(m_showTreeManager);
    m_ui->toolBar->addAction(m_ui->actionHelp);
    label = new QLabel(tr("Question"));
    m_ui->toolBar->addWidget(label);
    m_questionStr = new QLineEdit("");
    m_questionStr->setMaximumWidth(200);
    m_questionStr->setPlaceholderText(tr("enter your question"));
    m_ui->toolBar->addWidget(m_questionStr);
    button = new QPushButton(tr("Get an answer"));
    connect(button,SIGNAL(clicked()),this,
            SLOT(readQuestions()));
    m_ui->toolBar->addWidget(button);


    //Свойства
    m_tableWidget = new QTableWidget(1,2,this);

    m_tableWidget->setMinimumSize(160,0);
    m_tableWidget->setMaximumSize(220,160000);
    m_ui->horizontalLayout->addWidget(m_tableWidget);
    m_ui->mainToolBar->addWidget(m_tableWidget);

    QStringList listLabelGeader;
    listLabelGeader.append(tr("Property"));
    listLabelGeader.append(tr("Value"));
    m_tableWidget->setHorizontalHeaderLabels(listLabelGeader);

    labelItem = new QTableWidgetItem(tr("Arc name"));
    m_tableWidget->setItem(0, 0, labelItem);

    m_textedit = new QComboBox(this);
    m_textedit->addItem("#имя");
    m_textedit->addItem("#объект");
    m_textedit->addItem("#агент");
    m_textedit->addItem("#место");
    m_textedit->addItem("#тема");
    m_textedit->addItem("#is_a");
    m_textedit->addItem("#is_instance");
    m_textedit->setInsertPolicy(QComboBox::InsertAtTop);
    m_textedit->setEditable(true);
    m_ui->mainToolBar->addWidget(m_textedit);
    m_textedit->setEnabled(false);
    connect(m_textedit,SIGNAL(editTextChanged(QString)), m_graphicsView->graphLogic(),SIGNAL(textEdgeChange(QString)));

    m_tableWidget->setCellWidget(0,1,m_textedit);
}

void MainWindow::refreash() {
    //Для перевода
    m_ui->menuFile->setTitle(tr("File"));
    m_ui->menuEdit->setTitle(tr("Edit"));
    m_ui->menuNet->setTitle(tr("Net"));
    m_ui->menuNode->setTitle(tr("Node"));
    m_ui->menuView->setTitle(tr("View"));
    m_ui->menuHelp->setTitle(tr("Help"));

    m_ui->actionNew->setText(tr("New"));
    m_ui->actionOpen->setText(tr("Open"));
    m_ui->actionSave->setText(tr("Save"));
    m_ui->actionSaveAs->setText(tr("Save as..."));
    m_ui->actionClose->setText(tr("Close"));
    m_ui->actionExport->setText(tr("Export"));
    m_ui->actionQuit->setText(tr("Quit"));
    m_ui->actionHelp->setText(tr("Help"));
    m_ui->actionAbout_QtMindMap->setText(tr("About"));

    m_addNode->setText(tr("Add node (ins)"));
    m_delNode->setText(tr("Del node (del)"));
    m_editNode->setText(tr("Edit node (F2, dubclick)"));
    m_scaleUpNode->setText(tr("ScaleUp Node (Ctrl +)"));
    m_scaleDownNode->setText(tr("ScaleDown Node (Ctrl -)"));
    m_nodeColor->setText(tr("Node color (c)"));

    m_nodeTextColor->setText(tr("Node textcolor (t)"));
    m_addEdge->setText(tr("Add edge (a)"));
    m_delEdge->setText(tr("Del edge (d)"));
    m_moveNode->setText(tr("Move node\n(Ctrl cursor, drag)"));
    m_subtree->setText(tr("Change on wholesubtree\n(Ctrl shift)"));
    m_zoomIn->setText(tr("Zoom in (+, scrollup)"));
    m_zoomOut->setText(tr("Zoom out (-, scrolldown)"));

    m_esc->setText(tr("Leave editing,\nedge eadd/remove (esc)"));
    m_hintMode->setText(tr("Hint mode (f)"));
    m_showMainToolbar->setText(tr("Show main toolbar\n(Ctrl m)"));
    m_showStatusIconToolbar->setText(tr("Insert status icons\n(Ctrl i)"));
    m_showTreeManager->setText(tr("Show tree manager"));
    m_hideTree->setText(tr("Hide tree manager"));
    m_startMlv->setText(tr("Run inference engine"));

    m_undo->setText(tr("&Undo"));
    m_redo->setText(tr("&Redo"));
    m_mainToolbar->setText(tr("main toolbar"));
    m_iconToolbar->setText(tr("icon toolbar"));
    m_undoToolbar->setText(tr("undo toolbar"));

    m_questionStr->setPlaceholderText(tr("enter your question"));
    button->setText(tr("Get an answer"));
    label->setText(tr("Question"));

    QStringList listLabelGeader;
    listLabelGeader.append(tr("Property"));
    listLabelGeader.append(tr("Value"));
    m_tableWidget->setHorizontalHeaderLabels(listLabelGeader);

    labelItem->setText(tr("Arc name"));
    m_treeWidget->setHeaderLabel(tr("Objects manager"));
}

void MainWindow::setEnglishLanguage() {
    QTranslator trans;
    trans.load("qtmindmap_nb_NO.qm");
    qApp->removeTranslator(&trans);
    refreash();
    setWindowTitle("Intelligent talk");
}

void MainWindow::setRussionLanguage() {
    QTranslator trans;
    trans.load("qtmindmap_nb_NO.qm");
    qApp->installTranslator(&trans);
    refreash();
    setWindowTitle("Интеллектуальный болтун");
}

void MainWindow::showTreeManager() {
    if(!m_treeWidget->isVisible()) {
        m_treeWidget->setVisible(true);
        m_hideTree->setEnabled(true);
        m_showTreeManager->setIcon(QIcon(":/networkhide.png"));
        m_graphicsView->graphLogic()->showTreeManager();
    }
    else hideTree();
}

void MainWindow::setupStatusIconToolbar()
{
    // map signals so actions can send icon name
    m_signalMapper = new QSignalMapper(this);

    m_insertIcon = new QAction(tr("Insert icon:"), this);
    m_insertIcon->setDisabled(true);

    m_doIt = new QAction(QIcon(":/applications-system.svg"), tr("&Do"), this);
    m_doIt->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D));
    connect(m_doIt, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_doIt, ":/applications-system.svg");

    m_trash = new QAction(QIcon(":/user-trash-full.svg"), tr("&Trash"), this);
    m_trash->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
    connect(m_trash, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_trash, ":/user-trash-full.svg");

    m_info = new QAction(QIcon(":/mail-attachment.svg"), tr("&Refer"), this);
    m_info->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
    connect(m_info, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_info, ":/mail-attachment.svg");

    m_blocked = new QAction(QIcon(":/dialog-warning.svg"), tr("&Blocked"),
                            this);
    m_blocked->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
    connect(m_blocked, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_blocked, ":/dialog-warning.svg");

    m_question = new QAction(QIcon(":/help-browser.svg"), tr("&How?"), this);
    m_question->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_H));
    connect(m_question, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_question, ":/help-browser.svg");

    m_postpone = new QAction(QIcon(":/x-office-calendar.svg"), tr("&Postpone"),
                             this);
    m_postpone->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
    connect(m_postpone, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_postpone, ":/x-office-calendar.svg");

    m_delegate = new QAction(QIcon(":/system-users.svg"), tr("&Comission"),
                             this);
    m_delegate->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
    connect(m_delegate, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_delegate, ":/system-users.svg");

    m_maybe = new QAction(QIcon(":/dialog-information.svg"), tr("ma&Ybe"),
                          this);
    m_maybe->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
    connect(m_maybe, SIGNAL(activated()), m_signalMapper, SLOT (map()));
    m_signalMapper->setMapping(m_maybe, ":/dialog-information.svg");

    connect(m_signalMapper, SIGNAL(mapped(const QString &)),
            m_graphicsView->graphLogic(), SLOT(insertPicture(const QString &)));
    connect(m_graphicsView->graphLogic(),SIGNAL(arcActivated(bool)),
            this,SLOT(arcEnabled(bool)));

    m_ui->statusIcons_toolBar->addAction(m_insertIcon);
    m_ui->statusIcons_toolBar->addAction(m_doIt);
    m_ui->statusIcons_toolBar->addAction(m_trash);
    m_ui->statusIcons_toolBar->addAction(m_info);
    m_ui->statusIcons_toolBar->addAction(m_blocked);
    m_ui->statusIcons_toolBar->addAction(m_question);
    m_ui->statusIcons_toolBar->addAction(m_postpone);
    m_ui->statusIcons_toolBar->addAction(m_delegate);
    m_ui->statusIcons_toolBar->addAction(m_maybe);
    m_ui->statusIcons_toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}

void MainWindow::arcEnabled(bool value) {
    m_textedit->setEnabled(value);
    //    m_textedit->clear();
}

void MainWindow::setupEditToolbar()
{
    m_undoStack = new QUndoStack(this);
    m_undoView = new QUndoView(m_undoStack,this);
    m_ui->undoToolBar->addWidget(m_undoView);


    m_undo = m_undoStack->createUndoAction(this, tr("&Undo"));
    m_undo->setShortcuts(QKeySequence::Undo);
    m_redo = m_undoStack->createRedoAction(this, tr("&Redo"));
    m_redo->setShortcuts(QKeySequence::Redo);
    m_ui->menuEdit->addAction(m_undo);
    m_ui->menuEdit->addAction(m_redo);

    m_ui->menuEdit->addSeparator();

    m_mainToolbar = new QAction(tr("main toolbar"), this);
    m_mainToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M));
    connect(m_mainToolbar, SIGNAL(activated()),
            this, SLOT (showMainToolbar()));

    m_iconToolbar = new QAction(tr("icon toolbar"), this);
    m_iconToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_I));
    connect(m_iconToolbar, SIGNAL(activated()),
            this, SLOT (showStatusIconToolbar()));

    m_undoToolbar = new QAction(tr("undo toolbar"), this);
    m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
    connect(m_undoToolbar, SIGNAL(activated()),
            this, SLOT (showUndoToolbar()));

    m_ui->menuEdit->addAction(m_mainToolbar);
    m_ui->menuEdit->addAction(m_iconToolbar);
    m_ui->menuEdit->addAction(m_undoToolbar);

    m_graphicsView->graphLogic()->setUndoStack(m_undoStack);
}

void MainWindow::setTitle(const QString &title)
{
    title.isEmpty() ?
                setWindowTitle("QtMindMap") :
                setWindowTitle(QString(title).append(" - QtMindMap"));
}

void MainWindow::readQuestions() {
    m_graphicsView->graphLogic()->startMLV(m_questionStr->text().trimmed().toLower());
}
